/////////////////////////////////////////////////////////////////////////
//
// Amuse Engine SDK - core/memory
// Copyright( c) 2013.  All Rights Reserved
//
// File:		AEMiniAllocator.h
// Author:		Gianluca Belardelli
// Date:		18/11/2013
//
/////////////////////////////////////////////////////////////////////////
#ifndef _AEMINIALLOCATOR_H_
#define _AEMINIALLOCATOR_H_

/// @brief
/// This code snippet provides a high speed micro-allocator.
/// The concept is that you reserve an initial bank of memory for small allocations.  Ideally a megabyte or two.
/// The amount of memory reserved is equal to chunkSize*6
/// All micro-allocations are split into 6 seperate pools.
///    They are: 0-8 bytes
///              9-32 bytes
///              33-64 bytes
///              65-128 bytes
///              129-256 bytes
///
/// On creation of the micro-allocation system you preserve a certiain amount of memory for each of these banks.
/// The user provides a heap interface to callback for additional memory as needed.
/// In most cases allocations are order-N and frees are order-N as well.
/// The larger a buffer you provide, the closer to 'order-N' the allocator behaves.
/// This kind of a micro-allocator is ideal for use with STL as it does many tiny allocations.
/// All allocations are 16 byte aligned (with the exception of the 8 byte allocations, which are 8 byte aligned every other one).

class AE_COREDLLIMPEXP AEMiniAllocator : public AEMemoryAllocator
{
// Members
private:
	class AEMiniAllocatorImpl *m_lpImpl;

protected:
public:

// Methods
private:
protected:
public:
	AE_DECLARE_PLACEMENT_ALLOCATOR();
	AEMiniAllocator( int nSize = 32 * 1024 );
	AEMiniAllocator( int nSize, AEMemoryAllocator *lpParentAlloc = NULL );

	~AEMiniAllocator( void ) {};

	bool Init( int nSize, AEMemoryAllocator *lpHeap = NULL );
	/// Allocate numBytes.
	/// Allocations of zero bytes may return either the null pointer or
	/// act as if numBytes was nonzero.
	void *BlockAlloc( AEUINT32 uiNumBytes, AEMemoryAlign maAlign = AEMEM_ALIGN16 );

	/// Free numBytes. This method needs to handle p being null, as well
	/// as matching the handling of zero byte allocations as described in blockAlloc.
	void BlockFree( void *lpPtr, AEUINT32 uiNumBytes );

	void GetMemoryStats( AEMemStats &msMemStats ) {}
	virtual AEINT32 GetAllocatedSize( const void *lpObj, AEINT32 nBytes );
};

#endif // _AEMINIALLOCATOR_H_
